package com.cloudptt.api.product.func.lbs.server;

import com.cloudptt.api.models.lbs.LocationData;
import com.cloudptt.api.product.config.lbs.LocationConfig;
import com.cloudptt.api.product.device.DeviceInfo;
import com.cloudptt.api.product.log.Log;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;

public class GpsLocServer  extends AbstractLocServer implements LocationListener
{

	private static final String TAG = "GpsLocServer";
	private static final String LOCATION_SETTING_ACTION
			= "com.dream.setting.LOCATION_SETTING";
	private static final String LOCATION_SETTING_KEY = "location_status";

//	private static long BaseDate = 0;
	private LocationManager gpsLocClient;
	private LocationConfig curLocalConfig = null;
	private LocationData curLocation = new LocationData(0,0);

	public GpsLocServer()
	{
//		BaseDate = System.currentTimeMillis();
//		Log.d("lq", "GpsLocServer start BaseDate = "+BaseDate);
	}

	public void startLBS(Context ctx,LocationConfig localConfig)
	{
		Log.i(TAG, "startLBS localConfig = " + localConfig);
		if(localConfig == null
				|| !localConfig.isOpenLocation())
		{
			return;
		}
		if(DeviceInfo.isPNC370()
			|| DeviceInfo.isPNC380()) {
			Intent mIntent = new Intent();
			mIntent.setAction(LOCATION_SETTING_ACTION);
			mIntent.putExtra(LOCATION_SETTING_KEY, true);
			ctx.sendBroadcast(mIntent);
		}

		this.curLocalConfig = localConfig.clone();

		gpsLocClient = (LocationManager) ctx.getSystemService(Service.LOCATION_SERVICE);
		if(gpsLocClient != null){
			gpsLocClient.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0, GpsLocServer.this, Looper.getMainLooper());
		}
		//Log.i(TAG, "startLBS gpsLocClient = " + gpsLocClient);

		if (gpsLocClient == null
				|| gpsLocClient.getProvider(LocationManager.GPS_PROVIDER) == null)
		{
			Log.i(TAG, "gpsLocClient == null");
			//return;
			//Log.d(TAG, "requestLocationUpdates");
			//int timeInterval = localConfig.getTimeInterval();
			//int distanceInterval = localConfig.getDistanceInterval();
			//gpsLocClient.requestLocationUpdates(
			//		LocationManager.GPS_PROVIDER, timeInterval*1000, 0, this);
			/*gpsLocClient.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, distanceInterval*1000, 0, this);*/

			//gpsLocClient.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, null, Looper.getMainLooper());
			//gpsLocClient.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0, null, Looper.getMainLooper());
		}

		Runnable myRunnable = new Runnable() {
			public void run() {
				Location currentLocation;
				int i = 0;
				while (true)
				{
					currentLocation = gpsLocClient
							.getLastKnownLocation(LocationManager.GPS_PROVIDER);
					if (currentLocation != null ) {
						Log.i(TAG, "get gps firest data success");
						return;
					}
					else if (i == 10) {
						Log.i(TAG, "get gps firest data failed");
						return;
					} else {
						i++;
//						Log.i(TAG, i + " ");
						try {
							Thread.sleep(1000);
						} catch (Exception e) {

						}
					}
				}
			}
		};

		Thread thread = new Thread(myRunnable);
		thread.start();
	}
	/**
	 * GPS位置变化上报
	 */
	@Override
	public void onLocationChanged(Location location) {
		//Log.i(TAG, "onReceiveLocation GPS  Latitude = " + location.getLatitude() + " Longitude = "+ location.getLongitude());
		/*if(lastestRptTime!=0 && !curLocation.isInvalid())
		{
			float[] results=new float[1];
			double lastLattitude = curLocation.getLattitude();
			double lastLongitude = curLocation.getLongitude();
			Location.distanceBetween(location.getLatitude(), location.getLongitude(), lastLattitude, lastLongitude, results);

			boolean isInvalid = isInvalidReportLocationData(curLocalConfig,lastestRptTime,results[0]);
			if(isInvalid)
			{
				Log.i(TAG, "onReceiveLocation GPS  Invalid");
				return;
			}
		}*/

		//Log.d("lq", "curLocation.updateLocationData");
		curLocation.updateLocationData(location.getLatitude(), location.getLongitude(), location.getAltitude());
	}
	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}
	public void updateConfig(Context ctx,LocationConfig localConfig)
	{
		Log.i(TAG, "updateConfig localConfig = " + localConfig);

		boolean isSame = curLocalConfig.isSameInterval(localConfig);
		Log.i(TAG, "updateConfig isSame = " + isSame);

		if(!isSame)
		{
			curLocalConfig = localConfig.clone();
			stopLBS();
			startLBS(ctx,localConfig);
		}
	}
	public void stopLBS()
	{
		Log.i(TAG, "stopLBS localConfig = " + curLocalConfig);
		if(curLocalConfig == null
				|| !curLocalConfig.isOpenLocation())
		{
			return;
		}
//		if(gpsLocClient != null)
//		{
//			gpsLocClient.removeUpdates(this);
//		}

		curLocation = new LocationData(0,0);
	}
	public LocationData getCurLocation() {
		curLocation = new LocationData(0,0);
		if(gpsLocClient != null)
		{
			Location location = gpsLocClient.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			if(location != null)
			{
				Log.i(TAG, "updateConfig location.getLatitude() = " + location.getLatitude() + " location.getLongitude() = " + location.getLongitude());
				curLocation.updateLocationData(location.getLatitude(), location.getLongitude(), location.getAltitude());
			}
		}
		return curLocation;
	}
//	@Override
//	public VitalData getCurVitalData() {
//		// TODO Auto-generated method stub
//		long curTime = System.currentTimeMillis();
//		Log.d("lq", "curTime = "+curTime);
//		int val = (int)(curTime - BaseDate) /1000;
//		Log.d("lq", "BaseDate = "+BaseDate);
//		Log.d("lq", "val = "+val);
//		return new VitalData(val,60,"23",80,"000");
//	}

}
